package com.guanzhi.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;

/**
 * SpringSecurity
 * <p> Package: com.guanzhi.controller </p>
 * <p> File: HelloController.java </p>
 * <p> Description:  (初探门径 SpringSecurity)</p>
 * <p> Date: 2024/3/16 21:48</p>
 *
 * @author 观之
 * @version 1.0 （代码版本）
 * @email <a href="mailto:guanzhi55634@aliyun.com">观之</a>（邮箱）
 * @date 2024/1/23 21:48
 * @since jdk17（jdk版本）
 */
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String helloWorld() {
        //获取上下文
        SecurityContext context = SecurityContextHolder.getContext();
        //获取认证信息
        Authentication authenticationContext = context.getAuthentication();
        //获取认证用户名
        String username = authenticationContext.getName();
        //用户身份信息
        Object principal = authenticationContext.getPrincipal();
        //用户权限
        Collection<? extends GrantedAuthority> authorities = authenticationContext.getAuthorities();
        return "Hello World";
    }
}
